import { ColumnType } from 'antd/es/table';
import { ModalProps, TableProps } from 'antd';
import React, { Dispatch, MutableRefObject, ReducerAction, RefObject } from 'react';
import { ButtonType } from 'antd/lib/button/button';
import type { inbiz as IPageInbiz } from '@inbiz/render';
import { DrawerProps } from 'antd/lib/drawer';
import { ActionEnum, ConnectorEnum } from './shared';
import {
  IModalCallBackEvents,
  IModalEmit,
} from '@inbiz/render';
import { OrderInfo } from '@inbiz/hooks';
import { UploadFile } from 'antd/lib/upload/interface';
import { inbiz } from '@inbiz/render';

export type SearchType = { searchable: boolean; scrollY?: number };

export interface ActionConfigType {
  id?: number | string;
  actionName: string;
  actionType: ActionEnum;
  openMode?: string;
  paramField?: string;
  page?: string;
  position?: string;
  icon?: { type: string; color: string };
  type?: string;
  windowName?: string;
  parentAction?: string;
  render?: (className?: string) => React.ReactNode;
}

export interface ActionsProps {
  data?: any;
  className?: string;
  actions?: ActionConfigType[];
  displayQty?: number;
  actionDisplaySetup?: string;
  readonly?: boolean;
  tableHeaderBtnShow?: string;
  inLineBtnShow?: string;
}

export interface ActionProps extends ActionConfigType {
  data?: any;
  buttonType?: ButtonType;
  className?: string;
  actionDisplaySetup?: string;
  loading?: boolean;
  operationflag?: string;
  updateLoading?: () => boolean;
  dispatch?: (state?: any, action?: any) => any;
}

export interface PropConfigType {
  id: string;
  columnName: string;
  propName: string;
  title?: string;
  label?: string;
  placeKey?: string;
  placeText?: string;
  order?: number;
  width?: number;
  sorter?: string;
  fixed?: 'left' | 'right' | 'none';
}

export type ModalConfig = {
  actions: ActionConfigType[];
  width: number;
  height: number;
};

export type ToolbarType = {
  actions?: ActionProps[];
  displayQty: number;
  actionDisplaySetup?: string;
};

export type QueryParam = {
  current: number;
  pageSize: number;
  orders?: OrderInfo[];
};

export type QueryParams = {
  pageInfo?: PageInfo;
  orders?: OrderInfo[];
  conditions?: ConditionsType[];
};

export interface TableModalProps extends ModalProps, InBizProps {
  data?: any;
  state?: any;
  openMode?: string;
  actionType?: string;
  actions?: ActionConfigType[];
  height?: number;
  widthSuffix?: '%' | 'px';
  heightSuffix?: '%' | 'px';
  onClose?: (param?: any) => void;
  dataSourceRef?: RefObject<RecordType[]>;
  tableCacheRef?: MutableRefObject<any>;
  primaryKey: string;
}

export interface TableDrawerProps extends Omit<DrawerProps, 'onClose'>, InBizProps {
  actionType?: string;
  openMode?: string;
  actions?: ActionProps[];
  widthSuffix?: '%' | 'px';
  heightSuffix?: '%' | 'px';
  dispatch?: (action: {}) => void;
  onClose?: (param?: any) => void;
  render?: (props: TableDrawerProps) => React.ReactNode;
}

export type SubWindowType = {
  width?: { widowWidth?: number; widthSuffix?: string };
  height: { windowHeight?: number; heightSuffix?: string };
  actionDisplaySetup?: string;
  tableHeaderBtnShow?: string;
  inLineBtnShow?: string;
};

export type ExportType = {
  fields?: any[];
  conditions?: any[];
  appId?: string;
  sourceModel?: string;
  fileName?: string;
  recordIds?: string;
  importUpdate?: boolean;
  pageInfo?: {
    index: number;
    size: number;
  };
  orderInfo?: {};
};

export type ImportType = {
  file: UploadFile;
  fields?: any[];
  appId?: string;
  sourceModel?: string;
  importUpdate?: boolean;
};

export type DownloadTmplType = {
  fields?: any[];
  appId?: string;
  sourceModel?: string;
  fileName?: string;
  importUpdate?: boolean;
};

export interface SubWindowConfigType {
  id: string;
  parentAction?: string;
}

export interface RecordType {
  [key: string]: any;
}
export interface CardTmplValueProps {
  name: string;
  id: string;
}
export type VisibleColumnType = { visible?: string } & ColumnType<RecordType>;

export type TitleConfig = { status?: boolean; value?: string };

export type ItemData = {
  placeKey: string;
  placeText: string;
  label: string;
  propName: string;
  order: number;
  id?: string;
};

export type ColumnConfig = {
  tableValues?: {
    actions?: ActionConfigType[];
    attrs?: PropConfigType[];
    subWindow?: ActionConfigType[];
  };
  formItems?: {
    [key: string]: any;
  };
  queryFields?: { value: string | number; label: string }[];
};

export type AlignType = 'left' | 'center' | 'right';

export type ButtonSize = 'middle' | 'large';

export type Expanded = 'expanded' | 'collapse';

export type ExpandedSize = 'all' | 'oneLine';

export interface SearchProps {
  fetchData?: () => void;
}

export type PageData = {
  data?: RecordType[];
  total?: number;
  primary?: string;
};

export interface TableTopProps {
  children?: React.ReactNode;
}

export type ScrollType = { x?: number | true | string; y?: number | string };

export type ConditionsType = {
  connector: ConnectorEnum;
  condition: any[];
  index: number;
};

export interface InBizProps {
  sourceModel?: string;
  appInfo?: any;
  sourcetype?: 'model' | string;
  modelkey?: string;
  inbiz?: IPageInbiz;
  page?: string;
  viewMode?:string;
  callBackEvents?: {
    onModalSaveModify?: (data: object, inbiz: any) => object //修改保存前事件
    onBeforeChange?: (v: string) => boolean
    // 列格式化
    onColumnsFormat: (
      value: string,
      record: object,
      index: number,
      dataIndex: string,
    ) => React.ReactNode;
    onEditRow: <T>(data: T, index: number) => T // 编辑行内数据时触发
  } & IModalCallBackEvents;
  emit?: IModalEmit & {
    (type: 'onEditInit', inbiz: any, id: string, dataIndex: string, index: number): void;
    (type: 'onEditEnd', dataIndex: string, value: unknown, index: number): void;
    (type:'onCurrentData',data:any,rowIndex:any, dataIndex:any):any;
  };
  pageRef?: RefObject<{ page: IPageInbiz }>;
}

export type QueryType = { currentPage?: number; pageSize?: number; params?: any; orders?: any };
export type PageInfo = { current: number; pageSize: number };

export type TableCtxType = InBizProps & {
  attrs?: PropConfigType[];
  isDesignable?: boolean;
  state?: any;
  sourceModel: string;
  modelKey: string;
  dispatch?: Dispatch<ReducerAction<any>>;
  importUpdate?: boolean;
  modalButtons?: {
    actions?: ActionConfigType[];
    width: number;
    height: number;
  };
  dataSourceRef?: RefObject<RecordType[]>;
  refresh: () => any;
  handlePrint: () => any;
  tableCacheRef: RefObject<any>;
  eventsRef?: RefObject<any>;
  emit?: Function;
};

export interface TablePreviewProps extends Omit<TableProps<RecordType>, 'title'>, InBizProps {
  boxHeight: string;
  autoLoad?: boolean;
  showLineNumber?: boolean; //显示行号
  viewMode?:string;//查看模式
  controlId?: string;
  modelkey?: string;
  isDesignable?: boolean; //是否设计器模式
  resizeColumnWidth?: boolean; //列宽调整,
  align?: AlignType; //列宽调整,
  mergeRow?: boolean; //合并行,
  search?: boolean;
  searchField?: boolean;
  searchExpanded?: Expanded;
  title?: (data: readonly RecordType[]) => React.ReactNode | TitleConfig;
  importUpdate?: boolean;
  zebraEffect?: boolean;
  columnConfig?: ColumnConfig;
  height?: number;
  inbiz?: inbiz;
  searchPlacehoder?: string;
  rowHeight: any;
  inlineEdit?: boolean;
  modelname?: string;
  selectComponent?: string;
  relationField?: string;
  readOnly?: boolean;
}

export type FormatColumnsType = {
  eventsRef?: RefObject<any>;
};
